#ifndef FILERAWREADER_H
#define FILERAWREADER_H

#include <stdio.h>
#include <stdlib.h>
#include <sys/stat.h>  // for file size

#include "Timer/timer.01.h"

using namespace std;

class FRR {
	
	static const uint MB=1<<20;
	static const uint KB=1<<10;
	
	
	public:
	
	Timer tr;
	uint filesize;
	double readspeed;
	char *mem;
	
	
	const char *filename;
	
	FRR():
	mem(0)
	{}

	int getFileSize(const char* filename){
		struct stat results;
		stat(filename, &results);
		return results.st_size;
	}

	char* read(const char* filename) {
		
		this->filename = filename;
		
		filesize = getFileSize(filename);
		if (mem) delete [] mem;
		mem = new char[filesize];
		
		FILE *fs = fopen(filename, "r");
		if (fs==NULL) {printf("\033[22;31merror: can't read the file\033[22;35m '%s'\033[22;30m\n", filename); return NULL;}
		
		tr.set();
		fread(mem, 1, filesize, fs); 
		tr.get();
		
		readspeed = filesize/tr.sin;
		
		fclose(fs);
		
		return mem;
	}
		
	void print() {
		printf("\n");
		printf("\033[22;34mfilename\033[22;30m: \033[22;32m%s\033[22;30m\n", filename);
		printf("\033[22;34mreadtime\033[22;30m: %g \033[22;37ms\033[22;30m\n", tr.sin);
		printf("\033[22;34mreadspeed\033[22;30m: %g \033[22;37mMB/s\033[22;30m\n", readspeed/MB);
		
		if (filesize>2*MB)	printf("\033[22;34mfilesize\033[22;30m: %g \033[22;37mMB\033[22;30m\n", (double)filesize/MB); else
		if (filesize>2*KB)	printf("\033[22;34mfilesize\033[22;30m: %g \033[22;37mKB\033[22;30m\n", (double)filesize/KB); else
									printf("\033[22;34mfilesize\033[22;30m: %g \033[22;37mbytes\033[22;30m\n", (double)filesize);
		
		printf("\n");
	}
		
};

#endif